package com.mlethe.android.demo.banner.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.banner.transformer.ScaleYTransformer;
import com.mlethe.library.widget.banner.BannerView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> dataList;

    public RecyclerAdapter(List<String> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 999999999) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
            return new BannerViewHolder(view);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner_text, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            List<String> list = new ArrayList<>();
            list.add("https://obs-newretail-app.obs.cn-east-2.myhuaweicloud.com/tidbitImg/20200102151201-662312150349377545.jpg");
            list.add("https://obs-newretail-app.obs.cn-east-2.myhuaweicloud.com/tidbitImg/20200102164706-662336076966260742.jpg");
            list.add("https://obs-newretail-app.obs.cn-east-2.myhuaweicloud.com/tidbitImg/20200102164713-662336106179588104.jpg");
            list.add("https://obs-newretail-app.obs.cn-east-2.myhuaweicloud.com/tidbitImg/20200102164718-662336126710706179.jpg");
            BannerView bannerView = ((BannerViewHolder) holder).bannerView;
            bannerView.setAdapter(new BannerImgAdapter(list));
        } else {
            ((ItemViewHolder)holder).textView.setText(dataList.get(position - 1));
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 999999999;
        }
        return super.getItemViewType(position);
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder {
        private BannerView bannerView;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            bannerView = itemView.findViewById(R.id.main_banner_view);
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view);
        }
    }
}
